package com.example.tnot.Teacher.BaseFragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;

import com.example.tnot.R;
import com.example.tnot.Teacher.ActivityConfig.TeacherHomeActivity;

public abstract class BaseBackFragment extends Fragment {
    protected OnBackPressedCallback onBackPressedCallback;
    public NavController navController;
    private AlertDialog.Builder alert_builder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 获取当前Fragment中的NavController（如果有）
        Fragment navHostFragment = getChildFragmentManager().findFragmentById(R.id.teaHome_nav_host_fragment);
        if (navHostFragment instanceof NavHostFragment) {
            navController = ((NavHostFragment) navHostFragment).getNavController();
        }

        // 创建并注册返回回调
        onBackPressedCallback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                handleBackPressed();
            }
        };
        requireActivity().getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
    }

    @Override
    public void onResume() {
        super.onResume();
        hideBottomNavigation();
    }

    protected void handleBackPressed() {
        // 优先使用Navigation组件的返回栈
        if (navController != null && navController.getCurrentDestination() != null) {
            Log.d("NavController", navController.toString());
            Log.d("NavControllerDestination", navController.getCurrentDestination() .toString());
            if (!navController.popBackStack()) {
                // Navigation返回栈为空，执行其他逻辑
                if (getActivity() instanceof TeacherHomeActivity) {
                    ((TeacherHomeActivity) getActivity()).showBottomNavigation();
                    ((TeacherHomeActivity) getActivity()).enableViewPagerSwipe();
                }

                // 禁用当前回调，让Activity级别的回调处理
                onBackPressedCallback.setEnabled(false);
                requireActivity().onBackPressed();
            }
        }
        else {
            // 没有Navigation组件，使用原来的逻辑
            if (getActivity() instanceof TeacherHomeActivity) {
                ((TeacherHomeActivity) getActivity()).showBottomNavigation();
                ((TeacherHomeActivity) getActivity()).enableViewPagerSwipe();
            }

            if (getParentFragmentManager().getBackStackEntryCount() > 0) {
                getParentFragmentManager().popBackStack();
            } else {
                onBackPressedCallback.setEnabled(false);
                requireActivity().onBackPressed();

            }
        }
    }

    protected void hideBottomNavigation() {
        if (getActivity() instanceof TeacherHomeActivity) {
            ((TeacherHomeActivity) getActivity()).hideBottomNavigation();
            ((TeacherHomeActivity) getActivity()).disableViewPagerSwipe();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 移除回调，避免内存泄漏
        if (onBackPressedCallback != null) {
            onBackPressedCallback.remove();
        }
    }
}